﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using WorseGame.Common.Configs;

namespace WorseGame.Common.GlobalNPCs
{
    public class TwinsResurrection : GlobalNPC
    {
        private int resurrectionTimer = 0;
        private const int ResurrectionTime = 1200;
        private int deadTwinID = -1;
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public override void AI(NPC npc)
        {
            if (NPCConfig.Instance.Twins)
            {
                if (npc.type == NPCID.Retinazer || npc.type == NPCID.Spazmatism)
                {
                    CheckTwinStatus(npc);
                }
            }
        }
        private void CheckTwinStatus(NPC currentTwin)
        {
            int otherTwinType = currentTwin.type == NPCID.Retinazer ? NPCID.Spazmatism : NPCID.Retinazer;
            bool otherTwinAlive = false;
            for (int i = 0; i < Main.maxNPCs; i++)
            {
                NPC npc = Main.npc[i];
                if (npc.active && npc.type == otherTwinType && npc.life > 0)
                {
                    otherTwinAlive = true;
                    resurrectionTimer = 0;
                    deadTwinID = -1;
                    break;
                }
            }
            if (!otherTwinAlive && currentTwin.life > 0)
            {
                resurrectionTimer++;
                if (resurrectionTimer % 60 == 0 && Main.netMode != NetmodeID.Server)
                {
                    float progress = (float)resurrectionTimer / ResurrectionTime;
                    string twinName = otherTwinType == NPCID.Retinazer ? Language.GetTextValue("Mods.WorseGame.NewText.Twins2") : Language.GetTextValue("Mods.WorseGame.NewText.Twins3");
                    Main.NewText($"{twinName}" + Language.GetTextValue("Mods.WorseGame.NewText.Twins1") + $"{progress:P0}", Color.OrangeRed);
                    for (int j = 0; j < 10; j++)
                    {
                        Dust dust = Dust.NewDustPerfect(currentTwin.Center + new Vector2(Main.rand.NextFloat(-100f, 100f), Main.rand.NextFloat(-100f, 100f)), DustID.LifeDrain, Vector2.Zero, 0, otherTwinType == NPCID.Retinazer ? Color.Red : Color.Green, 2f);
                        dust.noGravity = true;
                    }
                }
                if (resurrectionTimer >= ResurrectionTime)
                {
                    ResurrectTwin(currentTwin, otherTwinType);
                    resurrectionTimer = 0;
                }
            }
        }
        private void ResurrectTwin(NPC livingTwin, int deadTwinType)
        {
            if (Main.netMode != NetmodeID.MultiplayerClient)
            {
                Vector2 spawnPosition = livingTwin.Center + new Vector2(Main.rand.NextFloat(-200f, 200f), Main.rand.NextFloat(-200f, 200f));
                int newTwin = NPC.NewNPC(livingTwin.GetSource_FromAI(), (int)spawnPosition.X, (int)spawnPosition.Y, deadTwinType);
                if (newTwin < Main.maxNPCs)
                {
                    NPC resurrectedTwin = Main.npc[newTwin];
                    resurrectedTwin.life = resurrectedTwin.lifeMax / 2;
                    if (resurrectedTwin.life < 1)
                    {
                        resurrectedTwin.life = 1;
                    }
                    resurrectedTwin.netUpdate = true;
                    for (int i = 0; i < 30; i++)
                    {
                        Dust dust = Dust.NewDustPerfect(resurrectedTwin.Center, DustID.Torch, new Vector2(Main.rand.NextFloat(-5f, 5f), Main.rand.NextFloat(-5f, 5f)), 0, deadTwinType == NPCID.Retinazer ? Color.Red : Color.Green, 3f);
                        dust.noGravity = true;
                    }
                    string twinName = deadTwinType == NPCID.Retinazer ? Language.GetTextValue("Mods.WorseGame.NewText.Twins2") : Language.GetTextValue("Mods.WorseGame.NewText.Twins3");
                    Main.NewText($"{twinName}" + Language.GetTextValue("Mods.WorseGame.NewText.Twins4"), Color.Red);
                    resurrectionTimer = 0;
                    deadTwinID = -1;
                }
            }
        }
        public override void OnKill(NPC npc)
        {
            if (NPCConfig.Instance.Twins)
            {
                if (npc.type == NPCID.Retinazer || npc.type == NPCID.Spazmatism)
                {
                    deadTwinID = npc.whoAmI;
                }
            }
        }
    }
}
